import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class EditData{
public static void main (String args[]){
final JButton btnJumlah;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
}
catch (Exception exc){
System.err.println("Error = " +exc);
}
final JFrame frame = new JFrame ("Edit Data Bujur Sangkar");
JLabel lSisi = new JLabel ("Sisi");
JLabel lLuas = new JLabel ("Luas");
final JTextField FieldSisi = new JTextField (20);
final JTextField FieldLuas = new JTextField (20);

JButton tombolCari = new JButton("Cari");
tombolCari.setMnemonic('C');
tombolCari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sql ="select * from dtbujursangkar where sisi_bujursangkar='" + FieldSisi.getText() + "'";
try {
Connection conn =DriverManager.getConnection("jdbc:odbc:bujursangkar");
Statement statement=conn.createStatement();
ResultSet rs=statement.executeQuery(sql);
if (rs.next()) {
FieldLuas.setText(rs.getString(2));
FieldLuas.requestFocus();
} else {
FieldLuas.setText("");
JOptionPane.showMessageDialog(frame,"sisi tidak ditemukan dalam database!","Cari Data",JOptionPane.WARNING_MESSAGE);
	FieldSisi.requestFocus();
	}
	statement.close();
	conn.close();
	}
	catch (Exception exc) {
	System.err.println("Error : " + exc);
	}
		}
			});
btnJumlah = new JButton("Hitung luas");
btnJumlah.setMnemonic('H');
btnJumlah.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
if (e.getSource () == btnJumlah)
{
int bilangan1 =(Integer.parseInt(FieldSisi.getText().trim()));
int hasil = bilangan1*bilangan1;
FieldLuas.setText(String.valueOf (hasil));
}
	}
		});

JButton tombolUpdate = new JButton("Update");
tombolUpdate.setMnemonic('U');
tombolUpdate.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
String sql = "update dtbujursangkar set Luas_bujursangkar ='" 
+ FieldLuas.getText() + "' where sisi_bujurSangkar='"
+ FieldSisi.getText().trim() + "'"; 
if (FieldSisi.getText().trim().equals("")) {
JOptionPane.showMessageDialog(frame, "sisi masih kosong!","Simpan data",JOptionPane.WARNING_MESSAGE);
FieldSisi.requestFocus();
}
else {
try {
Connection conn = DriverManager.getConnection("jdbc:odbc:bujursangkar");
Statement statement=conn.createStatement();
statement.executeUpdate(sql);
statement.close();
FieldSisi.setText("");
FieldLuas.setText("");
FieldSisi.requestFocus();
}
catch (Exception exc) {
System.err.println(sql);
System.err.println("Error:" + exc);
}
}
}
});

JButton tombolClear = new JButton("Clear");
tombolClear.setMnemonic ('c');
tombolClear.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
FieldSisi.setText("");
FieldLuas.setText("");
FieldSisi.requestFocus();
}
});
Container konten;
konten=frame.getContentPane();
konten.setLayout(new GridBagLayout());
GridBagConstraints pos = new GridBagConstraints();
pos.anchor=GridBagConstraints.WEST;
pos.gridx=5;
pos.gridy=5;
konten.add(lSisi,pos);
pos.gridx++;
konten.add(FieldSisi,pos);
pos.gridy ++;
pos.gridx=5;
konten.add(lLuas,pos);
pos.gridx ++;
konten.add(FieldLuas,pos);
pos.gridy ++;
konten.add(btnJumlah,pos);
pos.gridx ++;
konten.add(tombolCari,pos);
pos.gridx ++;
konten.add(tombolClear,pos);
pos.gridy++;
konten.add(tombolUpdate,pos);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
